package com.hongyun.tms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.hongyun.tms.common.PageResponseDto;
import com.hongyun.tms.common.constants.DefaultCreateByConstant;
import com.hongyun.tms.common.dto.dislog.DisLogExportDto;
import com.hongyun.tms.common.dto.proddislog.TmsProdDisLogQueryDto;
import com.hongyun.tms.common.utils.MapperingUtils;
import com.hongyun.tms.common.vo.TmsProdDisLogVO;
import com.hongyun.tms.entity.TmsProdDisLog;
import com.hongyun.tms.mapper.TmsProdDisLogMapper;
import com.hongyun.tms.service.TmsProdDisLogService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 调度日志
 *
 * @author yuheng
 * @since 1.0.0 2024-06-12
 */
@Service
@AllArgsConstructor
public class TmsProdDisLogServiceImpl extends ServiceImpl<TmsProdDisLogMapper, TmsProdDisLog> implements TmsProdDisLogService {

    @Override
    public PageResponseDto<TmsProdDisLogVO> page(TmsProdDisLogQueryDto query) {
        Page<TmsProdDisLog> page = new Page<>(query.getStartPage(), query.getPageSize());
        baseMapper.selectPage(page, getWrapper(query));
        return new PageResponseDto(page, TmsProdDisLogVO.class);
    }

    private LambdaQueryWrapper<TmsProdDisLog> getWrapper(TmsProdDisLogQueryDto query){
        LambdaQueryWrapper<TmsProdDisLog> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(org.apache.commons.lang3.StringUtils.isNotBlank(query.getClassCode()), TmsProdDisLog::getClassCode, query.getClassCode())
                .eq(query.getShiftDate() != null,TmsProdDisLog::getShiftDate,query.getShiftDate())
                .orderByDesc(TmsProdDisLog::getShiftDate);
        return wrapper;
    }

    @Override
    public void save(TmsProdDisLogVO vo) {
        TmsProdDisLog entity = MapperingUtils.copyProperties(vo,TmsProdDisLog.class);
        if(entity != null){
            entity.setId(IdWorker.getId());
            entity.setCreateBy(DefaultCreateByConstant.userId);
            entity.setCreateByName(DefaultCreateByConstant.userId.toString());
            entity.setCreateTime(LocalDateTime.now());
        }
        baseMapper.insert(entity);
    }

    @Override
    public void update(TmsProdDisLogVO vo) {
        TmsProdDisLog entity = MapperingUtils.copyProperties(vo,TmsProdDisLog.class);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    public List<DisLogExportDto> export(TmsProdDisLogQueryDto query) {
        long currentPage = 1;
        List<DisLogExportDto> allRecords = Lists.newArrayList();

        while (true) {
            query.setStartPage(currentPage);
            Page<TmsProdDisLog> page = new Page<>(query.getStartPage(), query.getPageSize());
            baseMapper.selectPage(page, getWrapper(query));
            PageResponseDto<DisLogExportDto> responseDto = new PageResponseDto(page, DisLogExportDto.class);
            List<DisLogExportDto> records = responseDto.getRecords();
            allRecords.addAll(records);
            if (records.size() < query.getPageSize()) {
                // 当前页的记录数小于 pageSize，说明已经是最后一页
                break;
            }
            currentPage++;
        }

        return allRecords;
    }
}
